
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class UsuarioCAD : BasicCAD, IUsuarioCAD
        {
        public UsuarioCAD() : base ()
        {
        }

        public UsuarioCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public UsuarioEN ReadOIDDefault (int id)
        {
                UsuarioEN usuarioEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        usuarioEN = (UsuarioEN)session.Load (typeof(UsuarioEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in UsuarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return usuarioEN;
        }


        public int New_ (UsuarioEN usuario)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (usuario.ColaDeReproduccion != null) {
                                session.Save (usuario.ColaDeReproduccion);
                        }
                        if (usuario.BandejaDeEntrada != null) {
                                session.Save (usuario.BandejaDeEntrada);
                        }

                        session.Save (usuario);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in UsuarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return usuario.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        UsuarioEN usuarioEN = (UsuarioEN)session.Load (typeof(UsuarioEN),
                                id

                                );
                        session.Delete (usuarioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in UsuarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        }
}
